﻿using Platform.Infrastructure;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Collections.Generic
{
    public static class DictionaryExtensions
    {


        public static object TryGetOrCreate( this System.Collections.IDictionary set, object key, Func<object> creator )
        {
            Arguments.IsNotNull( "set", set );

            if ( set.Contains( key ) )
            {
                return set[ key ];
            }
            else
            {
                return set[ key ] = creator();
            }
        }

        public static TModel TryGetOrCreate<TKey, TModel>( this IDictionary<TKey, TModel> set, TKey key, Func<TModel> creator )
        {
            Arguments.IsNotNull( "set", set );

            if ( set.ContainsKey( key ) )
            {
                return set[ key ];
            }
            else
            {
                return set[ key ] = creator();
            }
        }
    }
}
